using System;
using System.Text;
using System.IO;

using Jeffrey.XFramework.XCrawl;

namespace Jeffrey.XFramework
{
    public static class Logger
    {
        private static readonly Object s_SyncRoot = new Object();

        public static Boolean Initialize(CrawlController cc, String path)
        {
            s_Controller = cc;
            /*
             * when path does not exist, just use the Console...
             */
            if (path == null || path == String.Empty)
                return true;

            s_LoggerPath = path;

            try
            {
                s_Swr = File.AppendText(path);

                if (s_Swr != null)
                {
                    s_Start = DateTime.Now;
                    s_Swr.WriteLine("Logger Started at : [" + s_Start.Month + "." +
                                                              s_Start.Day + "." +
                                                              s_Start.Year + " " +
                                                              s_Start.Hour + ":" +
                                                              s_Start.Minute + ":" +
                                                              s_Start.Second + "]\r\n"
                                                           );
                }
                else
                    return false;

            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return false;
            }

            return true;
        }

        public static void Release()
        {
            LogInfo("The whole Task Ended...\n");

            if (s_Swr != null)
            {
                s_Swr.Flush();
                s_Swr.Close();
            }
        }

        public static void LogInfo(String info, String thread_name)
        {
            /*
             * TODO something associate with the spider here...
             */
            LogInfo(info);
        }

        public static void LogInfo(String info)
        {
            //DateTime dt = DateTime.Now;

            lock (s_SyncRoot)
            {
                if (s_Swr == null)
                {
                    Console.WriteLine(info);/* + " [" + dt.Month + "." +
                                                    dt.Day + "." +
                                                    dt.Year + " " +
                                                    dt.Hour + ":" +
                                                    dt.Minute + ":" +
                                                    dt.Second + "]\n"
                                                   );*/
                }
                else
                {
                    try
                    {
                        s_Swr.WriteLine(info);/* + " [" +  dt.Month + "." +
                                                       dt.Day + "." +
                                                       dt.Year + " " +
                                                       dt.Hour + ":" +
                                                       dt.Minute + ":" +
                                                       dt.Second + "]\r\n"
                                                       );*/
                    }
                    catch (IOException ioe)
                    {
                        Console.WriteLine(ioe.Message);
                        return;
                    }
                }
            }
        }

        public static void LogError(Exception e, String info)
        {
            lock (s_SyncRoot)
            {
                if (s_Swr == null)
                {
                    Console.Write(e.Message + " ");
                }
                else
                {
                    try
                    {
                        s_Swr.Write(e.Message + " ");
                    }
                    catch (IOException ioe)
                    {
                        Console.WriteLine(ioe.Message);
                        return;
                    }
                }
            }

            //this may cause sycronized problem...
            LogError(info);
        }

        public static void LogError(String info)
        {
            lock (s_SyncRoot)
            {
                if (s_Swr == null)
                {
                    Console.WriteLine(info);
                }
                else
                {
                    try
                    {
                        s_Swr.WriteLine(info);
                    }
                    catch (IOException ioe)
                    {
                        Console.WriteLine(ioe.Message);
                        return;
                    }
                }
            }
        }

        public static String LoggerPath
        {
            get { return s_LoggerPath; }
        }


        private static String s_LoggerPath;
        private static StreamWriter s_Swr;
        private static DateTime s_Start;
        private static CrawlController s_Controller;
    }
}
